<template>
    <a-modal :title="(formData.switch == 1 ? $t('Close') : $t('开启')) + ' OAuth2 ' + $t('服务端')" v-model='isShow'
        @cancel='handleClose' width='30%' @ok='handleCreate' :cancelText="$t('Cancel')" :okText="$t('Sure')"
        :maskClosable='false' v-dragModal>
        <a-spin :spinning="loading">
            <div class="ml-30">
                <span v-if="formData.switch == 1">{{ $t('确认关闭') }} 【{{ formData.auth_name }}】 OAuth2 {{ $t('服务端') }}{{ $t('吗？') }}</span>
            <span v-if="formData.switch == 0 && !isFlag">{{ $t('确认开启') }}【{{ formData.auth_name }}】OAuth2 {{ $t('服务端')
                }}{{ $t('吗？') }}</span>
            <div v-if="isFlag && formData.switch == 0" class="ml-16">
                <p>{{ $t('当前已开启 3 个 OAuth2 服务端。') }}</p>
                <p>{{ $t('如果开启该服务端，请先关闭一个已开启的服务端。') }}</p>
            </div>
            </div>
        </a-spin>
    </a-modal>
</template>

<script>

export default {
    data() {
        return {
            isShow: false,
            formData: {},
            isFlag: false,
            loading: false,
        };
    },
    methods: {
        async handleCreate() {
            if (this.isFlag && this.formData.switch == 0) {
                this.handleClose()
            }
            else {
                this.loading = true
                const params = {
                    auth_name: this.formData.auth_name,
                    auth_url: this.formData.auth_url,
                    get_token_url: this.formData.get_token_url,
                    get_user_url: this.formData.get_user_url,
                    login_out_url: this.formData.login_out_url,
                    client_id: this.formData.client_id,
                    client_secret: this.formData.client_secret,
                    redirect_uri: this.formData.redirect_uri,
                    access_type: this.formData.access_type,
                    role_id: this.formData.role_id,
                    auth_section_key: this.formData.auth_section_key,
                    switch: this.formData.switch === 0 ? 1 : 0
                }
                const { data } = await this.$api.oauth.modifyQAuth(params)
                this.model(data.id) //返回模板
                this.notice(data.id, data.params, data.flag) //通知列表显示
                if (data.flag === 0) {
                    this.handleClose()
                    // this.$message.success(msg);
                    this.$emit('get-list');
                } 
                else {
                    this.handleClose()
                }
                this.loading = false
            }
        },
        handleClose() {
            this.isShow = false
        }
    }

}
</script>